package com.travelsmart.client.apresentacao.view.subview;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.travelsmart.client.apresentacao.controller.CommandController;
import com.travelsmart.client.apresentacao.i18n.I18nMessages;
import com.travelsmart.client.apresentacao.view.BaseView;
import com.travelsmart.client.apresentacao.widget.FormFlexTable;
import com.travelsmart.client.apresentacao.widget.FormSimplePopupPanel;
import com.travelsmart.client.bean.to.HotelTO;
import com.travelsmart.client.bean.to.QuartoTO;

//# EXERC. 4 - TODO: Exercicio 4
public class ViewListQuarto extends BaseView {

	private CommandController<QuartoTO> handler;
	private final VerticalPanel vPanelViewListQuarto = new VerticalPanel();

	public ViewListQuarto(CommandController<QuartoTO> handler) {
		super(new HTML());
		this.handler = handler;
		initWidget(vPanelViewListQuarto);
	}

	public void exec(final HotelTO hotel) {
		vPanelViewListQuarto.clear();
		final FormFlexTable quartoFormFlexTable = new FormFlexTable(
				new HTML(""), new HTML(I18nMessages.INSTANCE.nome_quarto()),
				new HTML(I18nMessages.INSTANCE.quantidade()), new HTML(
						I18nMessages.INSTANCE.informacao()), new HTML(
						I18nMessages.INSTANCE.descricao()), new HTML(
						I18nMessages.INSTANCE.detalhes()), new HTML(
						I18nMessages.INSTANCE.preco()), new HTML(""));
		vPanelViewListQuarto.add(quartoFormFlexTable);
		List<QuartoTO> listaQuartos = hotel.getListRoom();
		// # INICIO - EXERC. 3
		if (listaQuartos != null) {
			for (int i = 0; i < listaQuartos.size(); i++) {
				final QuartoTO quarto = listaQuartos.get(i);
				quartoFormFlexTable.addRow(i, quarto.getNome(), new HTML(
						"<img src='" + quarto.getImagens() + "'>"), new HTML(
						quarto.getNome()), new HTML(String.valueOf(quarto
						.getQtiDisponivel())), new HTML(quarto.getInfo()),
						new HTML(quarto.getDescricao()),
						new FormSimplePopupPanel(
								new HTML(quarto.getDetalhes()), new HTML(
										I18nMessages.INSTANCE.detalhes())),
						new HTML(quarto.getPrecoString()), new Button(
								"<span id='button_reserva_quarto_" + i + "'>"
										+ I18nMessages.INSTANCE.reserva()
										+ "</span>", new ClickHandler() {
									public void onClick(ClickEvent event) {
										handler.handleSuccess(quarto);
									}
								}));
			}
		}
		// FIM - EXERC. 3
	}

}